import pygame, sys, os
from pygame.locals import *
from tools import *

class Player(pygame.sprite.Sprite):
    TYPE = 1
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_frame('res\\tank.bmp', (8, 8), (0, 0, 0))[0]
        self.rect = self.image.get_rect(topleft = pos)
        
class Enemy(pygame.sprite.Sprite):
    TYPE = 2
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_frame('res\\tank.bmp', (8, 8), (50, 0, 0))[0]
        sf = pygame.Surface((32, 32))
        pygame.draw.rect(sf, (50, 0, 0), sf.get_rect())
        self.image.blit(sf, (0, 0), sf.get_rect(), BLEND_ADD)
        self.rect = self.image.get_rect(topleft = pos)
        
class Brick(pygame.sprite.Sprite):
    TYPE = 3
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_image('res\\brick.bmp', (0, 0, 0))
        self.rect = self.image.get_rect(topleft = pos)   

class Steel(pygame.sprite.Sprite):
    TYPE = 5
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_image('res\\steel.bmp', (0, 0, 0))
        self.rect = self.image.get_rect(topleft = pos)

class Lawn(pygame.sprite.Sprite):
    TYPE = 6
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_image('res\\lawn.bmp', (0, 0, 0))
        self.rect = self.image.get_rect(topleft = pos)

class River(pygame.sprite.Sprite):
    TYPE = 7
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_image('res\\river.bmp', (0, 0, 0))
        self.rect = self.image.get_rect(topleft = pos)

class Ice(pygame.sprite.Sprite):
    TYPE = 8
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_image('res\\ice.bmp', (0, 0, 0))
        self.rect = self.image.get_rect(topleft = pos)        
class Base(pygame.sprite.Sprite):
    TYPE = 4
    def __init__(self, pos, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        self.image = load_frame('res\\base.bmp', (2, 1), (0, 0, 0))[0]
        self.rect = self.image.get_rect(topleft = pos)   

class Sel(pygame.sprite.Sprite):
    def __init__(self, *g):
        pygame.sprite.Sprite.__init__(self, *g)
        sf = pygame.Surface((16, 16))
        sf.set_colorkey((0, 0, 0))
        pygame.draw.lines(sf, (50, 255, 50), 1, ((0, 0), (14, 0), (14, 14), (0, 14)), 2)
        self.image = sf
        self.rect = pygame.Rect(0, 0, 16, 16)
    def setnode(self, node):
        self.rect = self.image.get_rect(topleft = node2pixel(node))
    def getnode(self):
        return pixel2node(self.rect.topleft)

def drawgrid(sf):
    for i in xrange(0, 640, 16):
        pygame.draw.line(sf, (100, 100, 100), (i, 0), (i, 640))
        pygame.draw.line(sf, (100, 100, 100), (0, i), (640, i))

def outgroup(comm, group):
    str = '\n#' + comm + '\n'
    count = 0
    for spr in group:
        x = pixel2node(spr.rect.topleft)[0]
        y = pixel2node(spr.rect.topleft)[1]
        str += "(%d, (%d, %d), ()), " % (spr.TYPE, x, y)
        count = (count + 1) % 5
        if count == 0: 
            str += '\n'
    return str
        


if sys.platform in ("win32", "win64"):
    os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
window = pygame.display.set_mode((640, 640))
clock = pygame.time.Clock()
sel = Sel()
render = pygame.sprite.RenderUpdates()
player = pygame.sprite.Group()
enemy = pygame.sprite.Group()
brick = pygame.sprite.Group()
steel = pygame.sprite.Group()
base = pygame.sprite.Group()
lawn = pygame.sprite.Group()
river = pygame.sprite.Group()
ice = pygame.sprite.Group()
ref = []
for i in xrange(40):
    ref.append([None] * 40)

if os.path.exists('rawlevel.py'):
    import rawlevel
    for item in rawlevel.raw:
        x = item[1][0]
        y = item[1][1]
        if item[0] == 1: ref[y][x] = Player(node2pixel((x, y)), render, player)
        elif item[0] == 2: ref[y][x] = Enemy(node2pixel((x, y)), render, enemy)        
        elif item[0] == 3: ref[y][x] = Brick(node2pixel((x, y)), render, brick)
        elif item[0] == 4: ref[y][x] = Base(node2pixel((x, y)), render, base)
        elif item[0] == 5: ref[y][x] = Steel(node2pixel((x, y)), render, steel)
        elif item[0] == 6: ref[y][x] = Lawn(node2pixel((x, y)), render, lawn)
        elif item[0] == 7: ref[y][x] = River(node2pixel((x, y)), render, river)
        elif item[0] == 8: ref[y][x] = Ice(node2pixel((x, y)), render, ice)
    
while 1:
    clock.tick(50)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
    window.fill((50, 50, 100))
    drawgrid(window)
    render.draw(window)
    window.blit(sel.image, sel.rect.topleft)
    pygame.display.update()

    mouse = pygame.mouse.get_pressed()
    if mouse[0]:
        sel.setnode(pixel2node(pygame.mouse.get_pos()))
        
    key = pygame.key.get_pressed()
    x = sel.getnode()[0]
    y = sel.getnode()[1]
    if key[K_0] or key[K_KP0]:
        if ref[y][x]: 
            ref[y][x].kill()
            ref[y][x] = None
    elif key[K_1] or key[K_KP1]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = Player(node2pixel((x, y)), render, player)
    elif key[K_2] or key[K_KP2]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = Enemy(node2pixel((x, y)), render, enemy)
    elif key[K_3] or key[K_KP3]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = Brick(node2pixel((x, y)), render, brick)
    elif key[K_4] or key[K_KP4]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = Base(node2pixel((x, y)), render, base)
    elif key[K_5] or key[K_KP5]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = Steel(node2pixel((x, y)), render, steel)
    elif key[K_6] or key[K_KP6]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = Lawn(node2pixel((x, y)), render, lawn)
    elif key[K_7] or key[K_KP7]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = River(node2pixel((x, y)), render, river)
    elif key[K_8] or key[K_KP8]:
        if ref[y][x]: ref[y][x].kill()
        ref[y][x] = Ice(node2pixel((x, y)), render, ice)
    elif key[K_RETURN]:
        out = open('rawlevel.py', 'w')
        out.write('raw = (')
        out.write(outgroup('player', player))
        out.write(outgroup('enemy', enemy))
        out.write(outgroup('brick', brick))
        out.write(outgroup('base', base))
        out.write(outgroup('steel', steel))
        out.write(outgroup('lawn', lawn))
        out.write(outgroup('river', river))
        out.write(outgroup('ice', ice))
        out.write('\n)')
        out.close()
    














